//
//  ChangePassWordController.m
//  敢唆嘛!
//
//  Created by lanou on 15/11/26.
//  Copyright © 2015年 zms. All rights reserved.
//

#import "ChangePassWordController.h"
#import "Device.h"
#import "Define.h"
@interface ChangePassWordController ()


@end

@implementation ChangePassWordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.navigationItem.title = @"修改密码";
    
    NSArray *placeArray = @[@"请输入原始密码", @"请输入新密码"];
    
    for (int i = 0; i<2; i++) {
        UITextField *oldText = [[UITextField alloc]initWithFrame:CGRectMake(50, KSHeight/6+i*60, KSWidth-100, 50)];
        
        oldText.placeholder = placeArray[i];
        
        oldText.secureTextEntry = YES;
        
        [self.view addSubview:oldText];
        
        UILabel *lineLabel = [[UILabel alloc]initWithFrame:CGRectMake(50, KSHeight/6+i*60+50, KSWidth-100, 1)];
        
        lineLabel.backgroundColor = [UIColor grayColor];
        
        [self.view addSubview:lineLabel];
        
    }
    
    //创建一个修改密码的按钮
    UIButton *exitButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    exitButton.frame = CGRectMake(KSWidth/4, KSHeight/2, KSWidth/2, 50);
    
    [exitButton.layer setCornerRadius:10];
    
    exitButton.backgroundColor = [UIColor greenColor];
    
    [exitButton setTitle:@"修改密码" forState:UIControlStateNormal];
    
    [exitButton addTarget:self action:@selector(exitButton) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:exitButton];
    
}

#pragma mark --修改密码的按钮触发的事件--
-(void)exitButton{
    
    UITextField *oldTextField = self.view.subviews[0];
    UITextField *newTextField = self.view.subviews[2];
    
    BmobUser *user = [BmobUser getCurrentUser];
    
    [user updateCurrentUserPasswordWithOldPassword:oldTextField.text newPassword:newTextField.text block:^(BOOL isSuccessful, NSError *error) {
        //如果成功
        if (isSuccessful) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"修改密码成功" preferredStyle:UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                 [self.navigationController popViewControllerAnimated:NO];
                
            }]];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
        //如果没修改成功
        else{
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"你的旧密码错误" preferredStyle:UIAlertControllerStyleAlert];
            
            [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
            
            [self presentViewController:alert animated:YES completion:nil];
        }
    }];
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITextField *oldTextField = self.view.subviews[0];
    UITextField *newTextField = self.view.subviews[2];
    
    [oldTextField resignFirstResponder];
    [newTextField resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
